﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.IO;

namespace DirectoryTreeXElement
{
    class Program
    {
        static string path = @"C:\Users\Milen\Desktop\Telerik";
        static void Main(string[] args)
        {

            string fileName = "dirs.xml";

            DirectoryInfo dir = new DirectoryInfo(path);
                XElement root = new XElement("dir",new XAttribute("name",dir.Name));
                DirSearch(dir, root);

                root.Save(fileName);

        }
        public static void DirSearch(DirectoryInfo sDir, XElement currElement)
        {
            XElement currDir = currElement;
            if (sDir.FullName != path)
            {
                currDir = new XElement("dir", new XAttribute("name", sDir.Name));
                currElement.Add(currDir);
            }
            
            foreach (FileInfo f in sDir.GetFiles())
            {

                currDir.Add(new XElement("file", new XAttribute("name", f.Name)));

            }
            foreach (DirectoryInfo d in sDir.GetDirectories())
            {

                DirSearch(d, currDir);
            }
        }

    }
}
